/**
 * @file 016.不含重复字符的最长子字符串.cc
 * @author snow-tyan (zziywang@163.com)
 * @brief {Life is too short to learn cpp.}
 * @version 0.1
 * @date 2021-12-09
 *
 * @copyright Copyright (c) 2021
 *
 */

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename T>
void print(const T &t)
{
    typename T::const_iterator it = t.begin();
    for (; it != t.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
}

class Solution
{
public:
    int lengthOfLongestSubstring(string s)
    {
        // 滑窗，窗口内字母打表
        int n = s.size();
        vector<int> cnt(128, 0);
        int i = 0, j = 0;
        int maxLen = 0;
        while (j < n) {
            // 如果当前字母不是第一次出现，移动窗口左端
            while (cnt[s[j]]) {
                --cnt[s[i++]];
            }
            ++cnt[s[j++]]; // 移动窗口右端
            maxLen = max(maxLen, j - i);
        }
        return maxLen;
    }
};

int main()
{
    cout << Solution().lengthOfLongestSubstring("abcabcbb") << endl;
    cout << Solution().lengthOfLongestSubstring("bbbbb") << endl;
    cout << Solution().lengthOfLongestSubstring("pwwkew") << endl;
    cout << Solution().lengthOfLongestSubstring("") << endl;
    cout << Solution().lengthOfLongestSubstring(" ") << endl;
    return 0;
}